package com.squirrelhuan.quickhttp_sample;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.squirrelhuan.quickhttp.util.HttpUtils;
import com.squirrelhuan.quickhttp.http.QuickHttp;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends Activity implements View.OnClickListener {
    Button btn_request_get;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_request_get = findViewById(R.id.btn_request_get);
        btn_request_get.setOnClickListener(this);
    }
    QuickHttp quickHttp;
    public void onClick(View v) {
        if(quickHttp==null) {
            quickHttp = new QuickHttp.Builder().setBaseUrl("https://www.baidu.com/")
                    //.addInterceptor(new QuickHttpLoggingInterceptor())
                    .build();
        }
        quickHttp.buildRetrofit(Api.class)
                .getVersion()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.v("onSubscribe", "onSubscribe");
                    }

                    @Override
                    public void onNext(Object o) {
                        Gson gson = new Gson();
                        String json = gson.toJson(o);

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.v("onError1", e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        Log.v("onComplete1", "onComplete");
                    }
                });
    }
    public void onClick1(View v) {
        HttpUtils.getInstance().getRetrofit(Api.class, "https://www.baidu.com/")
                .getSession()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.v("onSubscribe", "onSubscribe");
                    }

                    @Override
                    public void onNext(Object o) {
                        Gson gson = new Gson();
                        String json = gson.toJson(o);

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.v("onError", e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        Log.v("onComplete", "onComplete");
                    }
                });
    }
}
